//
//  SearchListView.swift
//  月升
//
//  Created by qinjiangtao on 2024/1/29.
//

import UIKit

class SearchListView: UIView {
    
    typealias modelBlock = (_ model: FoodModel)->()
    var selectModel: modelBlock?
    var model = NutritionAndHealthModel()
    private var searchTextF = UITextField()
    private var foodList = [FoodModel]()

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .black.withAlphaComponent(0.7)
        addUI()
    }
    
    private func addUI() {
        
        let titleL = UILabel().then({
            $0.frame = CGRect(x: 0, y: 30, width: SCREEN_WIDTH, height: 30)
            $0.text = "名称搜索"
            $0.textAlignment = .center
            $0.textColor = .white
            $0.font = SYSFONT(ofSize: 16)
        })
        self.addSubview(titleL)
        
        let leftView = UIView()
        leftView.frame = CGRect(x: 0, y: 0, width: 34, height: 34)
        let searchImgView = UIImageView().then {
            $0.frame = CGRect(x: 10, y: 10, width: 14, height: 14)
            $0.image = UIImage(named: "首页搜索")
            $0.contentMode = .scaleAspectFill
        }
        leftView.addSubview(searchImgView)
        searchTextF = UITextField().then {
            $0.frame = CGRect(x: 35, y: titleL.bottom + 15, width: SCREEN_WIDTH - 70, height: 34)
            $0.font = SYSFONT(ofSize: 12)
            $0.placeholder = "请输入搜索内容"
            $0.backgroundColor = ColorEEEEEE
            $0.returnKeyType = .search
            $0.delegate = self
            $0.leftView = leftView
            $0.leftViewMode = .always
            $0.addRoundedCorners(withRadius: 17)
        }
        self.addSubview(searchTextF)
        
        tableView.frame = CGRect(x: 0, y: searchTextF.bottom + 20, width: SCREEN_WIDTH, height: 500)
        self.addSubview(tableView)
        
    }
    
    /// 搜索食物
    private func searchFoodData() {
        let searchStr = searchTextF.text ?? ""
        QINetworkManager.requestJson(urlStr: "/Check/healthSearch", method: .get, parameters: [ "keyword":searchStr], success: { json in
            print(json)
            self.foodList.removeAll()
            let modeArr = json.arrayObject?.kj.modelArray(FoodModel.self) ?? [FoodModel]()
            self.foodList = modeArr
            self.tableView.reloadData()
        })
    }
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
        tableView.backgroundColor = .white
        tableView.delegate = self
        tableView.dataSource = self
        if #available(iOS 15.0, *) {
            tableView.sectionHeaderTopPadding = 0
        } else {}
        return tableView
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension SearchListView: UITableViewDataSource, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        foodList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.selectionStyle = .none
        cell.textLabel?.text = foodList[indexPath.row].name
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model = foodList[indexPath.row]
        if selectModel != nil {
            selectModel!(model)
            QINPopUpTool.sharedInstance().clear()
        }
    }
    
}

extension SearchListView: UITextFieldDelegate {
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        searchFoodData()
        return true
    }
    
}
